/*  
	Project: 	TweenPool
				Tracks active tweens
	Copyright (C) 2009, Jerome Maurey-Delaunay, all rights reserved.
	contact jerome(at)wemakedotcoms.com
	
	
	Legal crap I see in most open source project (and makes this look official):
	Redistribution and use in source and binary forms, with or without modification,
	are permitted provided that the following conditions are met:
	
	  * Redistribution's of source code must retain the above copyright notice,
		this list of conditions and the following disclaimer.
	
	  * Redistribution's in binary form must reproduce the above copyright notice,
		this list of conditions and the following disclaimer in the documentation
		and/or other materials provided with the distribution.
	
	This software is provided by the copyright holders and contributors "as is" and
	any express or implied warranties, including, but not limited to, the implied
	warranties of merchantability and fitness for a particular purpose are disclaimed.
	In no event shall I or contributors be liable for any direct,
	indirect, incidental, special, exemplary, or consequential damages
	(including, but not limited to, procurement of substitute goods or services;
	loss of use, data, or profits; or business interruption) however caused
	and on any theory of liability, whether in contract, strict liability,
	or tort (including negligence or otherwise) arising in any way out of
	the use of this software, even if advised of the possibility of such damage.
*/


package com.wemakedotcoms.tweenPool
{
	import com.wemakedotcoms.tweenPool.TweenPoolEvent;
	
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import gs.TweenMax;
	
	[Event(name="allTweensCompleted",	type="TweenPoolEvent")]
	[Event(name="tweenAdded",			type="TweenPoolEvent")]
	[Event(name="tweenRemoved",			type="TweenPoolEvent")]
	
	public class TweenPool extends EventDispatcher
	{	
		//Holds array of tweens
		private var _activeTweens:Array = [];
		
		private var _tweenCheckTimer:Timer = new Timer(60);
		
		public function TweenPool(target:IEventDispatcher = null)
		{
			super(target);
		}
		
		
		//
		// Public Methods
		//
		
		public function addTweenToPool( target:Object, duration:Number, vars:Object, type:String = "to" ) : uint
		{
			var tween:TweenMax;
			var index:uint = _activeTweens.length;
			
			switch(type){
				
				case "to":
					tween = TweenMax.to(target, duration, vars);
					break;
				
				case "from":
					tween = TweenMax.from(target, duration, vars);
					break;
					
				default:
					tween = TweenMax.to(target, duration, vars);
					
			}
			
			// adds the tween to the pool
			_activeTweens.push(tween);
			
			// starts the check timer if it is not running and adds event listener
			if(!_tweenCheckTimer.running){
				_tweenCheckTimer.addEventListener(TimerEvent.TIMER, checkTimerHandler, false, 0, true);
				_tweenCheckTimer.start();
			}
			
			// dispatch Tween added event
			dispatchEvent(new TweenPoolEvent(TweenPoolEvent.TWEEN_ADDED, index));
			
			return index;
		}
		
		
		public function get activeTweensCount():uint
		{
			var count:uint = 0;
			
			for(var i:uint ; i < _activeTweens.length ; ++i)
			{
				if(_activeTweens[i] != null)
				{
					count++;
				}
			}
			
			return count;
		}
		
		
		public function get activeTweensArray() : Array
		{
			return _activeTweens;
		}
		
		
		
		//
		// Event Handlers
		//
		
		private function checkTimerHandler( event : TimerEvent ) : void
		{
			for(var i:uint ; i < _activeTweens.length ; ++i)
			{
				if(_activeTweens[i] != null)
				{
					if(!TweenMax(_activeTweens[i]).enabled)
					{
						_activeTweens[i] = null;
						dispatchEvent( new TweenPoolEvent(TweenPoolEvent.TWEEN_REMOVED, i));
						
						//trace("Removed " + i);
						//trace("Tweens left " + activeTweensCount);
					}
				}
			}
			
			
			if(activeTweensCount == 0){
				// dispatch Tweens Completed
				dispatchEvent(new TweenPoolEvent(TweenPoolEvent.ALL_TWEENS_COMPLETED, 0));
				
				// resets array
				_activeTweens = [];
				
				// resets the timer and cleans up the event listener
				_tweenCheckTimer.reset();
				_tweenCheckTimer.removeEventListener(TimerEvent.TIMER, checkTimerHandler);
			} 
					
		}
	}
}